﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyOA.Model;
using System.Data;
using System.Data.SqlClient;

namespace MyOA.DAL
{
    public class RecycleBinService
    {
        DBHelper db = new DBHelper();
        public IList<FileInfo> GetAllDeletedFiles()
        {
            List<FileInfo> list = new List<FileInfo>();
            int FileTypeId = 0;
            int FileOwnerId = 0;
            try
            {
                DataTable table = db.getDataTable("sp_selectRecycleFile");

                foreach (DataRow row in table.Rows)
                {
                    FileInfo fileInfo = new FileInfo();

                    fileInfo.FileId = (int)row["FileId"];
                    fileInfo.FilesName = (string)row["FilesName"];
                    FileTypeId = (int)row["FileTypeid"];
                    fileInfo.FileType = GetFileTypeById(FileTypeId);
                    FileOwnerId = (int)row["FileOwnerid"];
                    fileInfo.FileOwner = GetUserInfoById(FileOwnerId);
                    fileInfo.Remark = (string)row["Remark"];

                    fileInfo.CreateDate = (DateTime)row["CreateDate"];
                    fileInfo.ParentId = (int)row["ParentId"];
                    fileInfo.FilePath = (string)row["FilePath"];
                    fileInfo.IfDelete = (int)row["IfDelete"];

                    list.Add(fileInfo);
                }

                return list;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        public FileTypeInfo GetFileTypeById(int id)
        {
            FileTypeInfo fileType = new FileTypeInfo();
            using (SqlDataReader reader = db.getDataReader("sp_getFileTypeById", new SqlParameter("@id", id)))
            {
                if (reader.Read())
                {

                    fileType.FileTypeName = (string)reader["filetypename"];
                    fileType.FileTypeImage = (string)reader["FileTypeImage"];
                    fileType.FileTypeSuffix = (string)reader["FileTypeSuffix"];

                }
            }
            db.CloseConnection();
            return fileType;
        }
        public UserInfo GetUserInfoById(int id)
        {
            UserInfo user = new UserInfo();
            using (SqlDataReader reader = db.getDataReader("sp_getUserById", new SqlParameter("@id", id)))
            {
                if (reader.Read())
                {
                    user.UserName = (string)reader["UserName"];

                }
            }
            db.CloseConnection();
            return user;
        }
        public bool ModifyFileInfoById(int fileid)
        {
            try
            {


                int result = db.getCount("sp_updateFileInfoById", new SqlParameter("@fileid", fileid));
                if (result > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        public bool DeleteFileInfoByFileId(int fileId)
        {


            try
            {


                int result = db.getCount("sp_deleteFileInfoById", new SqlParameter("@fileId", fileId));
                if (result > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
    }
}
